package com.example.demoexample;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.demoexample.activity.ClientActivity;
import com.example.demoexample.activity.ObservableActivity;
import com.example.demoexample.activity.RoundPhotoActivity;
import com.example.demoexample.activity.ServiceActivity;
import com.example.demoexample.activity.ViewActivity;
import com.example.demoexample.camera.ServerListeningThread;
import com.example.demoexample.communication.SwapInfoActivity;
import com.example.demoexample.model.BaseDto;
import com.example.demoexample.net.NetworkWrapper;
import com.example.demoexample.operate.ObservableTest;
import com.example.demoexample.thread.ClientManager;
import com.example.demoexample.utils.MathUtils;
import com.example.demoexample.utils.StringUtil;
import com.example.demoexample.view.Calender.ClendarView;
import com.example.demoexample.view.PageView;
import com.example.demoexample.view.recycleview.RecycleActivity;
import com.example.demoexample.viewpage.ViewPageActivity;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;


public class MainActivity extends AppCompatActivity {

    @BindView(R.id.btn_communication)
    Button btnCommunication;
    @BindView(R.id.btn_communication2)
    Button btnCommunication2;
    @BindView(R.id.tv_text)
    TextView tvText;
    @BindView(R.id.btn_recycleview)
    Button btnRecycle;
    @BindView(R.id.btn_client)
    Button btnClient;
    @BindView(R.id.btn_service)
    Button btnService;
    @BindView(R.id.btn_viewpage)
    Button btnViewpage;
    @BindView(R.id.btn_observable)
    Button btnObservable;
    @BindView(R.id.btn_viewActivity)
    Button btnViewActivity;
    @BindView(R.id.tv_paoma)
    TextView tvPaoma;
    @BindView(R.id.btn_loopviewpagerActivity)
    Button btnLoopviewpagerActivity;


    private ClendarView clendarView;
    private PageView pageview;
    private MathUtils mathUtils;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        tvText.setText("点击不了");

        //获取wifi服务
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        //判断wifi是否开启
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        String ip = intToIp(ipAddress);
        Log.e("MainActivity", "ip = " + ip);

        mathUtils = new MathUtils();
        math();

        testNet();
        test();

        hannuota(6, "A", "B", "C");
    }

    //    通过Java反射查看类信息
    private void test() {
        try {
//            1.使用Class类的forName(String clazzName)静态方法。该方法需要传入字符串参数，该字符串参数的值是某个类的全限定名（必须添加完整包名）。
//            2.调用某个类的class属性来获取该类对应的Class对象。
//            3.调用某个对象的getClass()方法。该方法是java.lang.Object类中的一个方法。

            //第一种方式 通过Class类的静态方法——forName()来实现
            Class<?> aClass = Class.forName("com.example.demoexample.operate");
            //第二种方式 通过类的class属性
            Class<ObservableTest> observableTestClass = ObservableTest.class;
            //第三种方式 通过对象getClass方法
            ObservableTest observableTest = new ObservableTest();
            Class<?> aClass1 = observableTest.getClass();

//            获取class对象的属性、方法、构造函数等
            Field[] declaredFields = aClass.getDeclaredFields();//获取class对象的所有属性
            Field[] fields = aClass.getFields();//获取class对象的public属性
            Field age = aClass.getDeclaredField("age"); //获取class指定属性
            Field des = aClass.getField("des");  //获取class指定的public属性
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    private void testNet() {
        NetworkWrapper.getInstance().login("", "").subscribe(new Subscriber<BaseDto>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.e(TAG, "e.getMessage = " + e.getMessage());
            }

            @Override
            public void onNext(BaseDto baseDto) {
                Log.e(TAG, "baseDto.getRet() = " + baseDto.getRet() + " baseDto.getMsg()=" + baseDto.getMsg());
            }
        });
    }

    private void math() {
        Log.e(TAG, "mathUtils.compareTo(5,7) = " + mathUtils.compareTo(5, 7));
        Log.e(TAG, "mathUtils.parseInt(\"444\",16) = " + mathUtils.parseInt("444", 16));
        Log.e(TAG, "mathUtils.parseInt(\"32\",10) = " + mathUtils.parseInt("32", 10));
        Log.e(TAG, "mathUtils.parseInt(\"444\",8) = " + mathUtils.parseInt("444", 8));

        StringUtil stringUtil = new StringUtil();
        Log.e(TAG, "wendeng，weideng compareTo = " + stringUtil.compareTo("wendeng", "weideng"));
        char[] dst = {'1', '2', '3', '4', '5', '6', '7', '8'};
        String s1 = new String("1234567");
        stringUtil.getChars("1234567", 2, 4, dst, 1);
        Log.e(TAG, "stringUtil.getChars = " + dst.length);
        System.out.print("拷贝的字符串为 dst：");
        System.out.println(dst);
        try {
            Log.e(TAG, "stringUtil.getBytes GBK = " + stringUtil.getBytes(dst.toString(), "GBK"));
            Log.e(TAG, "stringUtil.getBytes UTF-8 = " + stringUtil.getBytes(dst.toString(), "UTF-8"));
            Log.e(TAG, "stringUtil.getBytes ISO-8859-1 = " + stringUtil.getBytes(dst.toString(), "ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


    }

    private String intToIp(int i) {
        return (i & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);
    }

    @OnClick({R.id.btn_communication, R.id.btn_communication2, R.id.btn_recycleview, R.id.btn_client})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_communication:
                startActivity(new Intent(this, SwapInfoActivity.class));
                break;
            case R.id.btn_communication2:
                Toast.makeText(this, "点击了！", Toast.LENGTH_SHORT).show();
                new ServerListeningThread();
                new ClientManager().startServer(this);
//                startActivity(new Intent(this, ScollActivity.class));
                break;
            case R.id.btn_recycleview:
                Intent intent = new Intent(this, RecycleActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_client:
                Intent intent2 = new Intent(this, ClientActivity.class);
                startActivity(intent2);
                break;
        }
        btnViewpage = findViewById(R.id.btn_viewpage);
        initListener();
    }

    private void initListener() {
        btnViewpage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, ViewPageActivity.class));
            }
        });
    }


    @OnClick(R.id.btn_service)
    public void onViewClicked() {
        Intent intent = new Intent(this, ServiceActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.btn_observable)
    public void onViewClicked2() {
        Intent intent = new Intent(this, ObservableActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.btn_viewActivity)
    public void onViewClicked3() {
        Intent intent = new Intent(this, ViewActivity.class);
        startActivity(intent);
    }

    private void hannuota(int n, String from, String inter, String to) {
        if (n == 1) {
            Log.e(TAG, "Disk 1 from "
                    + from + " to " + to);
        } else {
            hannuota(n - 1, from, to, inter);
            Log.e(TAG, "Disk "
                    + n + " from " + from + " to " + to);
//            Log.e(TAG,"Disk "
//                    + n + " from " + from + " to " + to);
            hannuota(n - 1, inter, from, to);
        }
    }

    @OnClick(R.id.btn_loopviewpagerActivity)
    public void onViewClicked4() {
        Intent intent = new Intent(this, RoundPhotoActivity.class);
        startActivity(intent);
    }
}
